import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class SnakeHead  extends GameObj{
    //定义蛇头的方向
    private String  direction = "right";

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public SnakeHead(Image img, int x, int y, GameFrame frame) {
        super(img, x, y, frame);
        //添加键盘监听事件
        this.frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }

    public void changeDirection(KeyEvent keyEvent){
        switch (keyEvent.getKeyCode()){
            case KeyEvent.VK_W:
                direction = (direction.equals("up")) ? direction : "up";
                img = GameUtils.upImg;
                break;
            case KeyEvent.VK_S:
                direction = (direction.equals("down")) ? direction : "down";
                img = GameUtils.downImg;
                break;
            case KeyEvent.VK_A:
                direction = (direction.equals("left")) ? direction : "left";
                img = GameUtils.leftImg;
                break;
            case KeyEvent.VK_D:
                direction = (direction.equals("right")) ? direction : "right";
                img = GameUtils.rightImg;
                break;
        }
    }

    public void move(){
        //蛇身移动
        //包引用冲突需要使用绝对引用
        java.util.List<SnakeBody> snakeBodyList = this.frame.snakeBodyList;
        //后一截蛇身的位置等于前一截蛇身位置
        for (int i = snakeBodyList.size() - 1; i >= 0; i--) {
            if(i == 0){
                //第一截蛇身的位置等于当前蛇头的位置
                snakeBodyList.get(0).x = this.x;
                snakeBodyList.get(0).y = this.y;
            }
            else {
                snakeBodyList.get(i).x = snakeBodyList.get(i-1).x;
                snakeBodyList.get(i).y = snakeBodyList.get(i-1).y;
                if (snakeBodyList.get(i).x == this.x && snakeBodyList.get(i).y == this.y){
                    GameFrame.state = 3;
                }
            }
        }
        //蛇头移动
        switch (direction){
            case "up":
                y -= height;
                y = (y < frame.step ? frame.frameSize - frame.step : y);
                break;
            case "down":
                y += height;
                y = (y > frame.frameSize - frame.step ? frame.step : y);
                break;
            case "right":
                x += width;
                x = (x > frame.frameSize - frame.step ? 0 :x);
                break;
            case "left":
                x -= width;
                x = (x < 0 ? frame.frameSize - frame.step :x);
                break;
            default:
                break;
        }
    }
    @Override
    public void paintSelf(Graphics g){
        super.paintSelf(g);
        //蛇吃食物
        FoodObj foodObj = this.frame.foodObj;
        if (foodObj.x == this.x && foodObj.y == this.y){
            this.frame.foodObj = this.frame.getRandomFood();
            this.frame.snakeBodyList.add(new SnakeBody(GameUtils.bodyImg,0,0,this.frame));
            GameFrame.score++;
        }
        if(GameFrame.score > 1){
            GameFrame.state = 4;
        }
        move();
    }
}
